<?php
/**
 * ScoreModel.class.php
 * 积分控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-03-01
 */
namespace Admin\Model;
use Think\Model;

class ScoreModel extends Model {
	protected 	$trueTableName 	= 't_score';
	public 	$errMsg 	= false;

	/**
	 * 获取积分列表
	 * @param 	$type 	积分类型
	 * @param 	$keyword 	关键词
	 * @param 	$timeS 	起始时间
	 * @param 	$timeE 	结束时间
	 * @param 	$page 	页码数
	 * @param 	&$pageHtml	页码HTMl
	 * @return  $list
	 */
	function getList($type, $keyword, $timeS, $timeE, $page, &$pageHtml){
		$where 	= '1 = 1';
		if($type)
			$where 	.= $type == 1 ? ' and a.score > 0' : ' and a.score < 0';
		if($keyword)
			$where 	.= ' and b.nickname like "'.$keyword.'%"';
		if($timeS)
			$where 	.= ' and a.create_time >= '.$timeS;
		if($timeE)
			$where 	.= ' and a.create_time <= '.$timeE;

		$list 	= $this->table('t_score a')
			->join('left join t_user b on a.user_id = b.id')
			->where($where)
			->page($page, C('PAGE_COUNT'))
			->cache(true)
			->order('a.id desc')
			->field('a.id, a.user_id, b.nickname, a.goods_type, a.score, a.create_time')
			->select();
		foreach($list as &$item){
			$item['create_date']	= date('Y-m-d', $item['create_time']);
			$item['goods_type_text']= C('SCORE_USE_TYPE.'.$item['goods_type']);
			$item['score_text']	= $item['score'] > 0 ? '收入' : '支出';
		}
		
		if($pageHtml){
			$count 	= $this->table('t_score a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)->cache(true)->count();

			$Page  = new \Think\Page($count, C('PAGE_COUNT'));
			$pageHtml 	= $Page->angular_show();
		}

		return $list;
	}
	

	/**
	 * 导出积分
	 * @param   $keyword 关键词
	 * @param   $timeS 开始时间
	 * @param   $timeE 结束时间
	 * @param   $start 开始位置 : 有效值：返回相关数据条目；false：返回总条目数
	 */
	function outExcel($type, $keyword, $timeS, $timeE, $start, $length){
		$where 	= '1 = 1';
		if($type)
			$where 	.= $type == 1 ? ' and a.score > 0' : ' and a.score < 0';
		if($keyword)
			$where 	.= ' and b.nickname like "'.$keyword.'%"';
		if($timeS)
			$where 	.= ' and create_time > '.$timeS;
		if($timeE)
			$where 	.= ' and create_time < '.$timeE;

		if($start === false){
			$count 	= $this->table('t_score a')
				->join('left join t_user b on a.user_id = b.id')
				->where($where)
				->cache(true)
				->count();
			return $count;
		}

		$list 	= $this->table('t_score a')
			->join('left join t_user b on a.user_id = b.id')
			->where($where)
			->order('a.id desc')
			->field('a.id, a.user_id, b.nickname, b.truename, a.goods_type, a.score, a.create_time')
			->cache(true)
			->limit($start, $length)
			->select();
		return $list;
	}

	/**
     * 积分获取
     * @param   $userId
     * @param   $goodsType
     * @param   $goodsId
     * @param   $score
     * @return  boolean
     */
    function addScore($userId, $goodsType, $goodsId, $score, $remark = null, $stopTrans = false){
        //检测及分是否重复获取
        $isExt  = $this
            ->where('user_id = %d and goods_type = %d and goods_id = %d', $userId, $goodsType, $goodsId)
            ->find();
        if($isExt)  return true;
        
        $info   = array(
            'user_id'   => $userId,
            'goods_type'    => $goodsType,
            'goods_id'  => $goodsId,
            'score' => abs($score),
            'create_time'   => time(),
            'remark'    => $remark
        );

        $stopTrans or $this->startTrans();

        if(!$this->add($info)){
            $stopTrans or $this->rollback();
            $this->errMsg   = '积分信息插入失败(t_score)!';
            return false;
        }

        //用户积分添加
        $dUser  = new \Wechat\Model\UserModel();
        $userInfo   = $dUser->getUserInfo($userId);
        $userInfo['score']  += $score;
        if(!$dUser->updateUserInfo($userId, $userInfo)){
            $stopTrans or $this->rollback();
            $this->errMsg   = $dUser->errMsg;
            return false;
        }

        $stopTrans or $this->commit();

        //推送模版消息
        $wxTempMsg  = new \Wechat\Util\WxTempMsg();
        $wxTempMsg->sendScoreTempMsg($userInfo, $score, $goodsType);

        return true;
    }
}